// Fill out your copyright notice in the Description page of Project Settings.

#include "SimpleAIController.h"
#include "BehaviorTree/BehaviorTree.h"
#include "BehaviorTree/BehaviorTreeComponent.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "BehaviorTree/Blackboard/BlackboardKeyType_Bool.h"
#include "BehaviorTree/Blackboard/BlackboardKeyType_Object.h"
#include "SimplePlayerState.h"
#include "SimpleGameMode.h"
#include "SimpleCharacter.h"
#include "SimpleAIPawn.h"
#include "SimpleWeapon.h"
#include "HealthComponent.h"



ASimpleAIController::ASimpleAIController(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	BlackboardComp = ObjectInitializer.CreateDefaultSubobject<UBlackboardComponent>(this, TEXT("BlackBoardComp"));
	BrainComponent = BehaviorComp = ObjectInitializer.CreateDefaultSubobject<UBehaviorTreeComponent>(this, TEXT("BehaviorComp"));
}

void ASimpleAIController::UpdateControlRotation(float DeltaTime, bool bUpdatePawn /*= true*/)
{
	FVector FocalPoint = GetFocalPoint();
	if (!FocalPoint.IsZero() && GetPawn())
	{
		FVector Direction = FocalPoint - GetPawn()->GetActorLocation();
		FRotator NewControlRotation = Direction.Rotation();

		NewControlRotation.Yaw = FRotator::ClampAxis(NewControlRotation.Yaw);

		SetControlRotation(NewControlRotation);

		APawn* const P = GetPawn();
		if (P && bUpdatePawn)
		{
			P->FaceRotation(NewControlRotation, DeltaTime);
		}

	}
}

void ASimpleAIController::GameHasEnded(class AActor* EndGameFocus /*= nullptr*/, bool bIsWinner /*= false*/)
{
	BehaviorComp->StopTree();
	StopMovement();
	SetEnemy(NULL);
}

void ASimpleAIController::ShootEnemy()
{
	ASimpleAIPawn* Bot = Cast<ASimpleAIPawn>(GetPawn());
	bool bCanShoot = false;
	ASimpleCharacter* Enemy = GetEnemy();
	if (Enemy && (!Enemy->bIsDying))
	{
		if (LineOfSightTo(Enemy, Bot->GetActorLocation()))
		{
			bCanShoot = true;
		}
		
	}
	if (bCanShoot)
	{
		Bot->CurrentWeapon->StartFire();
	}
}

void ASimpleAIController::Possess(class APawn* InPawn)
{
	Super::Possess(InPawn);
	ASimpleAIPawn* Bot = Cast<ASimpleAIPawn>(InPawn);
	if (Bot && Bot->BotBehavior)
	{
		if (Bot->BotBehavior->BlackboardAsset)
		{
			BlackboardComp->InitializeBlackboard(*Bot->BotBehavior->BlackboardAsset);
		}
		EnemyKeyID = BlackboardComp->GetKeyID("Enemy");
		BehaviorComp->StartTree(*(Bot->BotBehavior));
	}
}

void ASimpleAIController::UnPossess()
{
	Super::UnPossess();
	BehaviorComp->StopTree();
}

void ASimpleAIController::Respawn()
{
	GetWorld()->GetAuthGameMode()->RestartPlayer(this);
}

void ASimpleAIController::SetEnemy(class APawn* InPawn)
{
	if (BlackboardComp)
	{
		BlackboardComp->SetValue<UBlackboardKeyType_Object>(EnemyKeyID,InPawn);
		SetFocus(InPawn);
	}
}

class ASimpleCharacter* ASimpleAIController::GetEnemy() const
{
	if (BlackboardComp)
	{
		return Cast<ASimpleCharacter>(BlackboardComp->GetValue<UBlackboardKeyType_Object>(EnemyKeyID));
	}
	return NULL;
}

void ASimpleAIController::UpdateAI()
{
	ASimpleAIPawn* Bot = Cast<ASimpleAIPawn>(this->GetPawn());
	if (Bot)
	{
		Bot->GetHealthComponent()->UpdateHealth(50.f);
		Bot->CurrentWeapon->UpdateProjectile(5.f, 0.2f);
		UE_LOG(LogClass, Warning, TEXT("AI Update"));
	}
}
